home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / xconf / files.c < prev    next >
C/C++ Source or Header  |  1995-01-06  |  1KB  |  63 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <limits.h>
  4. #include "../misc/misc.h"
  5. #include "xconf.h"
  6.  
  7. /*
  8.     Read a file holding a list of option (one line per option)
  9.     Return the number of options placed in tbopt with strdup().
  10.     Return -1 if any error. The caller must call free() for every
  11.     item in tbopt[].
  12. */
  13. int xconf_readflist(
  14.     const char *fname,
  15.     char *tbopt[],
  16.     int maxopt)        // Maximum number of line allowed in tbopt[]
  17. {
  18.     int ret = -1;
  19.     char path[PATH_MAX];
  20.     strcpy (path,fname);
  21.     FILE *fin = fopen (path,"r");
  22.     if (fin == NULL){
  23.         sprintf (path,USR_LIB_XCONF "/%s",fname);
  24.         fin = fopen (path,"r");
  25.     }
  26.     if (fin == NULL){
  27.         xconf_error (
  28.             "The file %s can't be found\n"
  29.             "It is normally found in " USR_LIB_XCONF "%s\n"
  30.             ,fname
  31.             );
  32.     }else{
  33.         char buf[300];
  34.         ret = 0;
  35.         while (fgets(buf,sizeof(buf)-1,fin)!= NULL){
  36.             str_strip (buf,buf);
  37.             /* #Specification: data files / option list
  38.                 Files holding options list are ascii files.
  39.                 They contain one option per line. They may have
  40.                 blank lines. Line beginning with ; are comments
  41.                 and will be silently skipped.
  42.  
  43.                 Trailing blanks are eliminated. Blanks at the
  44.                 beginning of the line are kept.
  45.  
  46.                 Option usally begin with a keyword and then a description.
  47.             */
  48.             if (buf[0] != ';' && buf[0] != '\0'){
  49.                 if (ret == maxopt){
  50.                     xconf_error (
  51.                         "Too many option in file %s\n",fname);
  52.                     break;
  53.                 }else{
  54.                     tbopt[ret++] = strdup(buf);
  55.                 }
  56.             }
  57.         }
  58.         fclose (fin);
  59.     }
  60.     return ret;
  61. }
  62.  
  63.